package com.rxyl.love.petdoctor.acticity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.app.MyApplication;
import com.rxyl.love.petdoctor.constant.Urls;
import com.rxyl.love.petdoctor.entity.DoctorDetailEntity;
import com.rxyl.love.petdoctor.entity.HospitalDetail;
import com.rxyl.love.petdoctor.util.NetworkUtil;

public class DoctorDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private int docId, hosId;
    private RatingBar ratingBar;
    private TextView docName, docJob, docHospital,docIntroduce;
    private SimpleDraweeView docIcon;
    private Button fansBtn, guanzhuBtn;
    private String docUrl, hosUrl, questionUrl, pingjiaUrl;
    private TextView solve, avg_score, good_total;
    private DoctorDetailEntity.DataEntity datas;
    private RelativeLayout relativeLayout;
    private HospitalDetail.DataEntity dataEntity;
    private ImageView docHosImage;
    private TextView hosName, docHosAddr, hosAvg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doctor_detail_layout);
        init();
        loadDocDatas();

    }

    /**
     * 初始化数据
     */
    private void init() {
        Intent intent = getIntent();
        docId = intent.getIntExtra("docId", -1);
        docName = (TextView) findViewById(R.id.doctor_detail_name);
        docJob = (TextView) findViewById(R.id.doctor_detail_job);
        docHospital = (TextView) findViewById(R.id.doctor_detail_address);
        docIntroduce= (TextView) findViewById(R.id.docIntro);
        docIcon = (SimpleDraweeView) findViewById(R.id.dcotor_detail_icon);
        ratingBar= (RatingBar) findViewById(R.id.ratingBar_docDetail_avg);
        fansBtn = (Button) findViewById(R.id.fansnumber);
        guanzhuBtn = (Button) findViewById(R.id.guanzhu);
        solve = (TextView) findViewById(R.id.solver_number);
        avg_score = (TextView) findViewById(R.id.tuijiecishu);
        good_total = (TextView) findViewById(R.id.good_pingjia);
        docHosImage = (ImageView) findViewById(R.id.doctor_detail_hospital);
        hosName = (TextView) findViewById(R.id.doctor_detail_hospital_name);
        docHosAddr = (TextView) findViewById(R.id.doctor_detail_hosaddress);
        hosAvg = (TextView) findViewById(R.id.hos_avg);
        relativeLayout = (RelativeLayout) findViewById(R.id.doc_detail_relative);

    }

    /**
     * 加载医生数据
     */
    private void loadDocDatas() {
        docUrl = Urls.DOCTOR_DETAIL + docId;
        NetworkUtil.getInstance().getFindDoctor(docUrl, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                Gson gson = new Gson();
                DoctorDetailEntity entity = gson.fromJson(responseInfo.result.toString(),
                        DoctorDetailEntity.class);
                if (entity != null & entity.getData() != null) {
                    datas = entity.getData();
                    setData();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

    /**
     * 加载医院数据
     */
    private void loadHospital() {
        hosUrl = Urls.HOSPITAL_DETAIL + hosId;
        NetworkUtil.getInstance().getHospital(hosUrl, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                Gson gson = new Gson();
                HospitalDetail hospitalDetail = gson.fromJson(responseInfo.result.toString(),
                        HospitalDetail.class);
                if (hospitalDetail != null && hospitalDetail.getData() != null) {
                    dataEntity = hospitalDetail.getData();
                    setHosData();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }

    /**
     * 医生信息
     */
    private void setData() {
        docName.setText(datas.getName());
        docHospital.setText(datas.getHospital());
        docJob.setText(datas.getJob());
        docIntroduce.setText(datas.getIntroduce());
        String url = datas.getAvatar();
        docIcon.setImageURI(Uri.parse(url));
        fansBtn.setText("粉丝" + datas.getFans_total() + "人");
        solve.setText(datas.getReply_total() + "");
        avg_score.setText(datas.getAvg_score() + "");
        good_total.setText(datas.getGood_total() + "");
        hosId = datas.getHospital_id();
        loadHospital();
        relativeLayout.setOnClickListener(this);
    }

    /**
     * 医院信息
     */
    private void setHosData() {
        hosName.setText(dataEntity.getName());
        String imageUrl = dataEntity.getPhoto_url();
        MyApplication.getApp().getImageLoader().displayImage(imageUrl, docHosImage,
                MyApplication.getApp().getImageOptions());
        docHosAddr.setText(dataEntity.getAddress());
        hosAvg.setText(dataEntity.getAvg_rating() + "分");
        ratingBar.setRating((float)dataEntity.getAvg_rating()/2);
    }

    public void backTo(View view) {
        this.finish();
    }

    /**
     * 医生所在医院的点击事件
     */
    @Override
    public void onClick(View v) {
        Intent intent=new Intent(this,HospitalActivity.class);
        intent.putExtra("hospitalId",hosId);
        startActivity(intent);
    }
}
